`:top
`!Generics`! are a facility of `F33f`_`[generic programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Generic_programming]`_`f that were added to the `F33f`_`[Java programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f in 2004 within version `F33f`_`[J2SE`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Platform,_Standard_Edition]`_`f 5.0. They were designed to extend Java's `F33f`_`[type system`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_system]`_`f to allow "a type or method to operate on objects of various types while providing compile-time type safety".`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] The aspect `*`F33f`_`[compile-time type safety`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile-time_type_safety]`_`f`* required that parametrically polymorphic functions are not implemented in the `F33f`_`[Java virtual machine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_virtual_machine]`_`f, since type safety is impossible in this case.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]`:cite-ref-footnotebloch2018123-125chapter-5-item-27-eliminate-unchecked-warnings-3-0[`F5bf`_`[3`#cite-note-footnotebloch2018123-125chapter-5-item-27-eliminate-unchecked-warnings-3]`_`f]
The `F33f`_`[Java collections framework`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_collections_framework]`_`f supports generics to specify the type of objects stored in a collection instance.
In 1998, `F33f`_`[Gilad Bracha`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Gilad_Bracha]`_`f, `F33f`_`[Martin Odersky`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Martin_Odersky]`_`f, David Stoutamire and `F33f`_`[Philip Wadler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Philip_Wadler]`_`f created Generic Java, an extension to the Java language to support generic types.`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f] Generic Java was incorporated in Java with the addition of `F33f`_`[wildcards`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Wildcard_(Java)]`_`f.
>>Contents
• `F0af`_`[Hierarchy and classification`#hierarchy-and-classification]`_`f
• `F0af`_`[Motivation`#motivation]`_`f
• `F0af`_`[Generic class definitions`#generic-class-definitions]`_`f
• `F0af`_`[Generic method definitions`#generic-method-definitions]`_`f
• `F0af`_`[Diamond operator`#diamond-operator]`_`f
• `F0af`_`[Type wildcards`#type-wildcards]`_`f
• `F0af`_`[Generics in throws clause`#generics-in-throws-clause]`_`f
• `F0af`_`[Problems with type erasure`#problems-with-type-erasure]`_`f
• `F0af`_`[Differences from Arrays`#differences-from-arrays]`_`f
• `F0af`_`[Covariance, contravariance and invariance`#covariance-contravariance-and-invariance]`_`f
• `F0af`_`[Reification`#reification]`_`f
• `F0af`_`[Project on generics`#project-on-generics]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Citations`#citations]`_`f
• `F0af`_`[References`#references]`_`f
-─
>>Hierarchy and classification
According to `*Java Language Specification`*:`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]
• A `!`F33f`_`[type variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_variable]`_`f`! is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
• A `!class`! is generic if it declares one or more type variables.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-0[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] It defines one or more type variables that act as parameters.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-0[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.
• An `!`F33f`_`[interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Interface_(Java)]`_`f`! is generic if it declares one or more type variables.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-1[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] It defines one or more type variables that act as parameters.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-2[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.
• A `!method`! is generic if it declares one or more type variables.`:cite-ref-footnotebloch2018135-138chapter-5-item-30-favor-generic-methods-8-0[`F5bf`_`[8`#cite-note-footnotebloch2018135-138chapter-5-item-30-favor-generic-methods-8]`_`f] These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.
• A `!constructor`! can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.
>>Motivation
The following block of Java code illustrates a problem that exists when not using generics. First, it declares an `!`B100`F9d9ArrayList`f`b`! of type `!`B100`F9d9Object`f`b`!. Then, it adds a `B100`F9d9String`f`b to the `B100`F9d9ArrayList`f`b. Finally, it attempts to retrieve the added `B100`F9d9String`f`b and cast it to an `B100`F9d9Integer`f`b—an error in logic, as it impossible to cast any string instance to an integer.
`B100`F9d9final List v = new ArrayList();`f`b
`B100`F9d9v.add("test"); // A String that cannot be cast to an Integer`f`b
`B100`F9d9final Integer i = (Integer) v.get(0); // Run time error`f`b
Although the code is compiled without error, it throws a runtime exception (`B100`F9d9java.lang.ClassCastException`f`b) when executing the third line of code. This type of `F33f`_`[logic error`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Logic_error]`_`f can be detected during compile time by using generics`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-3[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] and is the primary motivation for using them.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-1[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] It defines one or more type variables that act as parameters.
The above code fragment can be rewritten using generics as follows:
`B100`F9d9final List<String> v = new ArrayList<String>();`f`b
`B100`F9d9v.add("test");`f`b
`B100`F9d9final Integer i = (Integer) v.get(0); // (type error) compilation-time error`f`b
The type parameter `B100`F9d9String`f`b within the angle brackets declares the `B100`F9d9ArrayList`f`b to be constituted of `B100`F9d9String`f`b (a descendant of the `B100`F9d9ArrayList`f`b's generic `B100`F9d9Object`f`b constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of `B100`F9d9v.get(0)`f`b is defined as `B100`F9d9String`f`b by the code generated by the compiler.
The logical flaw in the third line of this fragment will be detected as a `F33f`_`[compile-time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile_time]`_`f error (with J2SE 5.0 or later) because the compiler will detect that `B100`F9d9v.get(0)`f`b returns `B100`F9d9String`f`b instead of `B100`F9d9Integer`f`b.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-4[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] For a more elaborate example, see reference.`:cite-ref-9[`F5bf`_`[9`#cite-note-9]`_`f]
Here is a small excerpt from the definition of the interfaces `!`B100`F9d9java.util.List`f`b`! and `!`B100`F9d9java.util.Iterator`f`b`! in package `!`B100`F9d9java.util`f`b`!:
`B100`F9d9interface List<E> {`f`b
`B100`F9d9`f`b
`B100`F9d9 void add(E x);`f`b
`B100`F9d9 Iterator<E> iterator();`f`b
`B100`F9d9`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9interface Iterator<E> {`f`b
`B100`F9d9`f`b
`B100`F9d9 E next();`f`b
`B100`F9d9 boolean hasNext();`f`b
`B100`F9d9`f`b
`B100`F9d9}`f`b
>>Generic class definitions
Here is an example of a generic Java class, which can be used to represent individual entries (key to value mappings) in a `F33f`_`[map`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Map_(computer_science)]`_`f:
`B100`F9d9public class Entry<KeyType, ValueType> {`f`b
`B100`F9d9`f`b
`B100`F9d9 private final KeyType key;`f`b
`B100`F9d9 private final ValueType value;`f`b
`B100`F9d9`f`b
`B100`F9d9 public Entry(KeyType key, ValueType value) {`f`b
`B100`F9d9 this.key = key;`f`b
`B100`F9d9 this.value = value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public KeyType getKey() {`f`b
`B100`F9d9 return key;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public ValueType getValue() {`f`b
`B100`F9d9 return value;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public String toString() {`f`b
`B100`F9d9 return "(" + key + ", " + value + ")";`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9}`f`b
This generic class could be used in the following ways, for example:
`B100`F9d9final Entry<String, String> grade = new Entry<String, String>("Mike", "A");`f`b
`B100`F9d9final Entry<String, Integer> mark = new Entry<String, Integer>("Mike", 100);`f`b
`B100`F9d9System.out.println("grade: " + grade);`f`b
`B100`F9d9System.out.println("mark: " + mark);`f`b
`B100`F9d9`f`b
`B100`F9d9final Entry<Integer, Boolean> prime = new Entry<Integer, Boolean>(13, true);`f`b
`B100`F9d9if (prime.getValue()) {`f`b
`B100`F9d9 System.out.println(prime.getKey() + " is prime.");`f`b
`B100`F9d9}`f`b
`B100`F9d9else {`f`b
`B100`F9d9 System.out.println(prime.getKey() + " is not prime.");`f`b
`B100`F9d9}`f`b
It outputs:
`B100`F9d9grade: (Mike, A)`f`b
`B100`F9d9mark: (Mike, 100)`f`b
`B100`F9d913 is prime.`f`b
>>Generic method definitions
Here is an example of a generic method using the generic class above:
`B100`F9d9public static <Type> Entry<Type, Type> twice(Type value) {`f`b
`B100`F9d9 return new Entry<Type, Type>(value, value);`f`b
`B100`F9d9}`f`b
Note: If we remove the first `B100`F9d9<Type>`f`b in the above method, we will get `F33f`_`[compilation error`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compilation_error]`_`f (cannot find symbol "Type"), since it represents the declaration of the symbol.
In many cases, the user of the method need not indicate the type parameters, as they can be inferred:
`B100`F9d9final Entry<String, String> pair = Entry.twice("Hello");`f`b
The parameters can be explicitly added if needed:
`B100`F9d9final Entry<String, String> pair = Entry.<String>twice("Hello");`f`b
The use of `F33f`_`[primitive types`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Primitive_type]`_`f is not allowed, and `F33f`_`[boxed`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_type_(object-oriented_programming)]`_`f versions must be used instead:
`B100`F9d9final Entry<int, int> pair; // Fails compilation. Use Integer instead.`f`b
There is also the possibility to create generic methods based on given parameters.
`B100`F9d9public <Type> Type[] toArray(Type... elements) {`f`b
`B100`F9d9 return elements;`f`b
`B100`F9d9}`f`b
In such cases you can't use primitive types either, e.g.:
`B100`F9d9Integer[] array = toArray(1, 2, 3, 4, 5, 6);`f`b
>>Diamond operator
Thanks to `F33f`_`[type inference`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_inference]`_`f, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets (`B100`F9d9<>`f`b, called the `*diamond operator`*) for a pair of angle brackets containing the one or more type parameters that a sufficiently close context `F33f`_`[implies`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_conversion]`_`f.`:cite-ref-10[`F5bf`_`[10`#cite-note-10]`_`f] Thus, the above code example using `B100`F9d9Entry`f`b can be rewritten as:
`B100`F9d9final Entry<String, String> grade = new Entry<>("Mike", "A");`f`b
`B100`F9d9final Entry<String, Integer> mark = new Entry<>("Mike", 100);`f`b
`B100`F9d9System.out.println("grade: " + grade);`f`b
`B100`F9d9System.out.println("mark: " + mark);`f`b
`B100`F9d9`f`b
`B100`F9d9final Entry<Integer, Boolean> prime = new Entry<>(13, true);`f`b
`B100`F9d9if (prime.getValue()) System.out.println(prime.getKey() + " is prime.");`f`b
`B100`F9d9else System.out.println(prime.getKey() + " is not prime.");`f`b
>>Type wildcards
A type argument for a parameterized type is not limited to a concrete class or interface. Java allows the use of "type wildcards" to serve as type arguments for parameterized types. Wildcards are type arguments in the form "`B100`F9d9<?>`f`b"; optionally with an upper or lower `F33f`_`[bound`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bounded_quantification]`_`f. Given that the exact type represented by a wildcard is unknown, restrictions are placed on the type of methods that may be called on an object that uses parameterized types.
Here is an example where the element type of a `B100`F9d9Collection<E>`f`b is parameterized by a wildcard:
`B100`F9d9final Collection<?> c = new ArrayList<String>();`f`b
`B100`F9d9c.add(new Object()); // compile-time error`f`b
`B100`F9d9c.add(null); // allowed`f`b
Since we don't know what the element type of `B100`F9d9c`f`b stands for, we cannot add objects to it. The `B100`F9d9add()`f`b method takes arguments of type `B100`F9d9E`f`b, the element type of the `B100`F9d9Collection<E>`f`b generic interface. When the actual type argument is `B100`F9d9?`f`b, it stands for some unknown type. Any method argument value we pass to the `B100`F9d9add()`f`b method would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is `F33f`_`[null`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Null_pointer]`_`f; which is a member of every type.`:cite-ref-11[`F5bf`_`[11`#cite-note-11]`_`f]
To specify the `F33f`_`[upper bound`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bounded_quantification]`_`f of a type wildcard, the `B100`F9d9extends`f`b keyword is used to indicate that the type argument is a subtype of the bounding class.`:cite-ref-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12-0[`F5bf`_`[12`#cite-note-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12]`_`f] So `B100`F9d9List<? extends Number>`f`b means that the given list contains objects of some unknown type which extends the `B100`F9d9Number`f`b class. For example, the list could be `B100`F9d9List<Float>`f`b or `B100`F9d9List<Number>`f`b. Reading an element from the list will return a `B100`F9d9Number`f`b. Adding null elements is, again, also allowed.`:cite-ref-13[`F5bf`_`[13`#cite-note-13]`_`f]
The use of wildcards above adds flexibility`:cite-ref-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12-1[`F5bf`_`[12`#cite-note-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12]`_`f] since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither `B100`F9d9List<Number>`f`b nor `B100`F9d9List<Integer>`f`b is a subtype of the other; even though `B100`F9d9Integer`f`b is a subtype of `B100`F9d9Number`f`b.`:cite-ref-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12-2[`F5bf`_`[12`#cite-note-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12]`_`f] So, any method that takes `B100`F9d9List<Number>`f`b as a parameter does not accept an argument of `B100`F9d9List<Integer>`f`b. If it did, it would be possible to insert a `B100`F9d9Number`f`b that is not an `B100`F9d9Integer`f`b into it; which violates type safety. Here is an example that demonstrates how type safety would be violated if `B100`F9d9List<Integer>`f`b were a subtype of `B100`F9d9List<Number>`f`b:
`B100`F9d9final List<Integer> ints = new ArrayList<>();`f`b
`B100`F9d9ints.add(2);`f`b
`B100`F9d9final List<Number> nums = ints; // valid if List<Integer> were a subtype of List<Number> according to substitution rule.`f`b
`B100`F9d9nums.add(3.14);`f`b
`B100`F9d9final Integer x = ints.get(1); // now 3.14 is assigned to an Integer variable!`f`b
The solution with wildcards works because it disallows operations that would violate type safety:
`B100`F9d9final List<? extends Number> nums = ints; // OK`f`b
`B100`F9d9nums.add(3.14); // compile-time error`f`b
`B100`F9d9nums.add(null); // allowed`f`b
To specify the lower bounding class of a type wildcard, the `B100`F9d9super`f`b keyword is used. This keyword indicates that the type argument is a supertype of the bounding class. So, `B100`F9d9List<? super Number>`f`b could represent `B100`F9d9List<Number>`f`b or `B100`F9d9List<Object>`f`b. Reading from a list defined as `B100`F9d9List<? super Number>`f`b returns elements of type `B100`F9d9Object`f`b. Adding to such a list requires either elements of type `B100`F9d9Number`f`b, any subtype of `B100`F9d9Number`f`b or `F33f`_`[null`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Null_pointer]`_`f (which is a member of every type).
The mnemonic PECS (Producer Extends, Consumer Super) from the book `!Effective Java`! by `F33f`_`[Joshua Bloch`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Joshua_Bloch]`_`f gives an easy way to remember when to use wildcards (corresponding to `F33f`_`[covariance`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Covariance_and_contravariance_(computer_science)]`_`f and `F33f`_`[contravariance`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Covariance_and_contravariance_(computer_science)]`_`f) in Java.`:cite-ref-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12-3[`F5bf`_`[12`#cite-note-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12]`_`f]
>>Generics in throws clause
Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:
`B100`F9d9public <T extends Throwable> void throwMeConditional(boolean conditional, T exception) throws T {`f`b
`B100`F9d9 if (conditional) {`f`b
`B100`F9d9 throw exception;`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
>>Problems with type erasure
Generics are checked at compile-time for type-correctness.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-5[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f] The generic type information is then removed in a process called `F33f`_`[type erasure`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_erasure]`_`f.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-2[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] For example, `B100`F9d9List<Integer>`f`b will be converted to the non-generic type `B100`F9d9List`f`b, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code uses the correct type.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-6[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f]
Because of type erasure, type parameters cannot be determined at run-time.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-3[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] For example, when an `B100`F9d9ArrayList`f`b is examined at runtime, there is no general way to determine whether, before type erasure, it was an `B100`F9d9ArrayList<Integer>`f`b or an `B100`F9d9ArrayList<Float>`f`b. Many people are dissatisfied with this restriction.`:cite-ref-14[`F5bf`_`[14`#cite-note-14]`_`f] There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an `B100`F9d9ArrayList`f`b contains an `B100`F9d9Integer`f`b, that ArrayList may have been parameterized with `B100`F9d9Integer`f`b (however, it may have been parameterized with any parent of `B100`F9d9Integer`f`b, such as `B100`F9d9Number`f`b or `B100`F9d9Object`f`b).
Demonstrating this point, the following code outputs "Equal":
`B100`F9d9final List<Integer> li = new ArrayList<>();`f`b
`B100`F9d9final List<Float> lf = new ArrayList<>();`f`b
`B100`F9d9if (li.getClass() == lf.getClass()) { // evaluates to true`f`b
`B100`F9d9 System.out.println("Equal");`f`b
`B100`F9d9}`f`b
Another effect of type erasure is that a generic class cannot extend the `B100`F9d9Throwable`f`b class in any way, directly or indirectly:`:cite-ref-15[`F5bf`_`[15`#cite-note-15]`_`f]
`B100`F9d9public class GenericException<T> extends Exception`f`b
The reason why this is not supported is due to type erasure:
`B100`F9d9try {`f`b
`B100`F9d9 throw new GenericException<Integer>();`f`b
`B100`F9d9}`f`b
`B100`F9d9catch (GenericException<Integer> e) {`f`b
`B100`F9d9 System.err.println("Integer");`f`b
`B100`F9d9}`f`b
`B100`F9d9catch (GenericException<String> e) {`f`b
`B100`F9d9 System.err.println("String");`f`b
`B100`F9d9}`f`b
Due to type erasure, the runtime will not know which catch block to execute, so this is prohibited by the compiler.
Java generics differ from `F33f`_`[C++ templates`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C++_Templates]`_`f. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown.
For example, the following code cannot be compiled:
`B100`F9d9<T> T instantiateElementType(List<T> arg) {`f`b
`B100`F9d9 return new T(); //causes a compile error`f`b
`B100`F9d9}`f`b
Because there is only one copy per generic class at runtime, `F33f`_`[static variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_variable]`_`f are shared among all the instances of the class, regardless of their type parameter. Consequently, the type parameter cannot be used in the declaration of static variables or in static methods.
Type erasure was implemented in Java to maintain `F33f`_`[backward compatibility`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Backward_compatibility]`_`f with programs written prior to Java SE5.`:cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-7[`F5bf`_`[7`#cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7]`_`f]
>>Differences from Arrays
There are several important differences between arrays (both primitive arrays and `B100`F9d9Object`f`b arrays), and generics in Java. Two of the major differences, namely, differences in terms of `F33f`_`[variance`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variance_(computer_science)]`_`f and `F33f`_`[reification`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Reification_(computer_science)]`_`f.
>>>Covariance, contravariance and invariance
Generics are invariant, whereas arrays are `F33f`_`[covariant`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Covariance_and_contravariance_(computer_science)]`_`f.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-4[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] This is a benefit of using generic when compared to non-generic objects such as arrays.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-5[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] Specifically, generics can help prevent run time exceptions by throwing a compile-time exception to force the developer to fix the code.
For example, if a developer declares an `B100`F9d9Object[]`f`b object and instantiates the object as a new `B100`F9d9Long[]`f`b object, no compile-time exception is thrown (since arrays are covariant).`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-6[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] This may give the false impression that the code is correctly written. However, if the developer attempts to add a `B100`F9d9String`f`b to this `B100`F9d9Long[]`f`b object, the program will throw an `B100`F9d9ArrayStoreException`f`b.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-7[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] This run-time exception can be completely avoided if the developer uses generics.
If the developer declares a `B100`F9d9Collection<Object>`f`b object an creates a new instance of this object with return type `B100`F9d9ArrayList<Long>`f`b, the Java compiler will (correctly) throw a compile-time exception to indicate the presence of incompatible types (since generics are invariant).`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-8[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] Hence, this avoids potential run-time exceptions. This problem can be fixed by creating an instance of `B100`F9d9Collection<Object>`f`b using `B100`F9d9ArrayList<Object>`f`b object instead. For code using Java SE7 or later versions, the `B100`F9d9Collection<Object>`f`b can be instantiated with an `B100`F9d9ArrayList<>`f`b object using the `F33f`_`[diamond operator`#diamond-operator]`_`f
>>>Reification
Arrays are `F33f`_`[reified`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Reification_(computer_science)]`_`f, meaning that an array object enforces its type information at run-time, whereas generics in Java are not reified.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-9[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f]
More formally speaking, objects with generic type in Java are non-reifiable types.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-10[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-11[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f]
Objects with generic type in Java are non-reifiable due to type erasure.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-12[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f] Java only enforces type information at compile-time. After the type information is verified at compile-time, the type information is discarded, and at run-time, the type information will not be available.`:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-13[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f]
Examples of non-reifiable types include `B100`F9d9List<T>`f`b and `B100`F9d9List<String>`f`b, where `B100`F9d9T`f`b is a generic formal parameter. `:cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-14[`F5bf`_`[6`#cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6]`_`f]
>>Project on generics
`F33f`_`[Project Valhalla`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Project_Valhalla_(Java_language)]`_`f is an experimental project to incubate improved Java generics and language features, for future versions potentially from Java 10 onwards. Potential enhancements include:`:cite-ref-16[`F5bf`_`[16`#cite-note-16]`_`f]
• `F33f`_`[generic specialization`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Generic_programming]`_`f, e.g. List<int>
• `F33f`_`[reified generics`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Comparison_of_C_Sharp_and_Java]`_`f; making actual types available at runtime.
>>See also
• `F33f`_`[Generic programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Generic_programming]`_`f
• `F33f`_`[Template metaprogramming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Template_metaprogramming]`_`f
• `F33f`_`[Wildcard (Java)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Wildcard_(Java)]`_`f
• `F33f`_`[Comparison of C# and Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Comparison_of_C_Sharp_and_Java]`_`f
• `F33f`_`[Comparison of Java and C++`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Comparison_of_Java_and_C++]`_`f
>>Citations
`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f Java Programming Language
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f A ClassCastException can be thrown even in the absence of casts or nulls."Java and Scala's Type Systems are Unsound" (PDF).
`:cite-note-footnotebloch2018123-125chapter-5-item-27-eliminate-unchecked-warnings-3`!3.`! `F0af`_`[↑`#cite-ref-footnotebloch2018123-125chapter-5-item-27-eliminate-unchecked-warnings-3-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f, pp. 123–125, Chapter §5 Item 27: Eliminate unchecked warnings.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f GJ: Generic Java
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f Java Language Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005
`:cite-note-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6`!6.`! `F0af`_`[↑`#cite-ref-footnotebloch2018126-129chapter-5-item-28-prefer-lists-to-arrays-6-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f, pp. 126–129, Chapter §5 Item 28: Prefer lists to arrays.
`:cite-note-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7`!7.`! `F0af`_`[↑`#cite-ref-footnotebloch2018117-122chapter-5-item-26-don-t-use-raw-types-7-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f, pp. 117–122, Chapter §5 Item 26: Don't use raw types.
`:cite-note-footnotebloch2018135-138chapter-5-item-30-favor-generic-methods-8`!8.`! `F0af`_`[↑`#cite-ref-footnotebloch2018135-138chapter-5-item-30-favor-generic-methods-8-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f, pp. 135–138, Chapter §5 Item 30: Favor generic methods.
`:cite-note-9`!9.`! `F0af`_`[↑`#cite-ref-9]`_`f `:citerefgilad-bracha2004`aGilad Bracha (July 5, 2004). "Generics in the Java Programming Language" (PDF). `*www.oracle.com`*.
`:cite-note-10`!10.`! `F0af`_`[↑`#cite-ref-10]`_`f "Type Inference for Generic Instance Creation".
`:cite-note-11`!11.`! `F0af`_`[↑`#cite-ref-11]`_`f `:citerefgilad-bracha2004`aGilad Bracha (July 5, 2004). "Generics in the Java Programming Language" (PDF). `*www.oracle.com`*. p. 5.
`:cite-note-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12`!12.`! `F0af`_`[↑`#cite-ref-footnotebloch2018139-145chapter-5-item-31-use-bounded-wildcards-to-increase-api-flexibility-12-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f, pp. 139–145, Chapter §5 Item 31: Use bounded wildcards to increase API flexibility.
`:cite-note-13`!13.`! `F0af`_`[↑`#cite-ref-13]`_`f `:citerefbracha`a`F33f`_`[Bracha, Gilad`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Gilad_Bracha]`_`f. "Wildcards > Bonus > Generics". `*The Java™ Tutorials`*. Oracle. ...The sole exception is null, which is a member of every type...
`:cite-note-14`!14.`! `F0af`_`[↑`#cite-ref-14]`_`f `:citerefgafter2006`aGafter, Neal (2006-11-05). "Reified Generics for Java". Retrieved 2010-04-20.
`:cite-note-15`!15.`! `F0af`_`[↑`#cite-ref-15]`_`f "Java Language Specification, Section 8.1.2". Oracle. Retrieved 24 October 2015.
`:cite-note-16`!16.`! `F0af`_`[↑`#cite-ref-16]`_`f `:citerefgoetz`aGoetz, Brian. "Welcome to Valhalla!". `*OpenJDK mail archive`*. OpenJDK. Retrieved 12 August 2014.
>>References
• `:citerefbloch2018`aBloch, Joshua (2018). `*"Effective Java: Programming Language Guide"`* (third ed.). Addison-Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0134685991.
`c`F0af`_`[↑ Back to top`#top]`_`f`a